package com.example.monitordata;

import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private  MyObserver mMyObserver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        mMyObserver = new MyObserver(new Handler());
        Uri uri = Uri.parse("content://com.example.contentobserverdb/info");
        getContentResolver().registerContentObserver(uri,true,mMyObserver);

    }
    private class MyObserver extends ContentObserver implements com.example.monitordata.MyObserver {

        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public MyObserver(Handler handler) {
            super(handler);
        }
        @Override
        public void onChange(boolean slfChange){
            Log.i("监测数据变化", "有人动了你的数据库！");
            super.onChange(slfChange);
        }
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        getContentResolver().unregisterContentObserver(mMyObserver);
    }
}